﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace REVISIONMAX30.Controls
{
    public partial class MyConnectionStringList : UserControl
    {
        public MyConnectionStringList()
        {
            InitializeComponent();
        }

        public UIHandler uiHandler { get; set; }
        public DataObjects.Group group { get; set; }

        public void Initzialize(UIHandler pUIHandler, DataObjects.Group pParentObject)
        {
            this.uiHandler = pUIHandler;
            this.group = pParentObject;
            this.UpdateViewer();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var tSelectedGuidList = this.lvConnectionStrings.SelectedItems.Cast<ListViewItem>().Select(t => t.Tag).Cast<Guid>().ToList();
            group.ConnectionStringList.RemoveAll(t => tSelectedGuidList.Contains(t.DataObjectGUID));

            this.UpdateViewer();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.Edit();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!this.txtName.Text.IsEmpty())
            {
                this.uiHandler.ViewConnectionString(new DataObjects.ConnectionString(this.txtName.Text), this.group);
                this.txtName.Text = "";
                this.UpdateViewer();
            }
            else
            {
                MessageBox.Show("Specify name for new connection string.");
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.UpdateViewer();
        }


        private void Edit()
        {
            if (this.lvConnectionStrings.SelectedItems.Count > 0)
            {
                var tSelectedGUID = (Guid)this.lvConnectionStrings.SelectedItems[0].Tag;
                this.uiHandler.ViewConnectionString(this.group.ConnectionStringList.Where(t => t.DataObjectGUID == tSelectedGUID).First(), this.group);
                this.UpdateViewer();
            }
        }

        private void lvConnectionStrings_DoubleClick(object sender, EventArgs e)
        {
            this.Edit();
        }

        public void UpdateViewer()
        {
            this.lvConnectionStrings.Items.Clear();

            foreach (var tConnectionString in this.group.ConnectionStringList)
            {
                var tvlItem = this.lvConnectionStrings.Items.Add(tConnectionString.Name);

                tvlItem.Tag = tConnectionString.DataObjectGUID;
                tvlItem.SubItems.Add(tConnectionString.Value);
                tvlItem.SubItems.Add(tConnectionString.Created.StringValueOrEmpty());
                tvlItem.SubItems.Add(tConnectionString.Updated.StringValueOrEmpty());

                if (tConnectionString.TestConnection().Severity == Frame.FrResultSeverityType.Error)
                {
                    tvlItem.BackColor = Color.LightSalmon;
                }
                else
                {
                    tvlItem.BackColor = Color.LightGreen;
                }

            }
        }

        private void btnDumpDBsToBackup_Click(object sender, EventArgs e)
        {
            this.group.DumpDBsToBackUp();
        }

        

    }
}
